package com.opennews.openplatform

import com.opennews.openplatform.constants.IncomeExpenseTypeConstant
import com.opennews.openplatform.constants.PaymentTypeConstant
import groovy.transform.CompileStatic
import org.springframework.beans.factory.annotation.Value

@CompileStatic
class DataInitializationController extends BaseController {
    @Value('${openplatform.appConfig.recentSearchListMaxCount}')
    Integer recentSearchListMaxCount

    @Value('${openplatform.baiduMap.androidKey}')
    String baiduMapAndroidKey

    @Value('${openplatform.amap.androidKey}')
    String amapAndroidKey

    @Value('${openplatform.amap.webApiKey}')
    String amapWebApiKey

    AccountGroupService accountGroupService
    BankCardService bankCardService
    IncomeExpenseDetailTypeService incomeExpenseDetailTypeService

    def index() {
        render("DataInitialization")
    }

    def retrieveAll() {
        def claims = getClaims()
        def accountGroupId = claims.accountGroupId
        def userId = claims.id
        def accountGroupInfo = accountGroupService.queryAccountGroupInfo(accountGroupId, userId)
        def bankCards = bankCardService.queryAllWithBasicInfoByAccountGroup(accountGroupId)
        def detailTypes = incomeExpenseDetailTypeService.queryByAccountGroup(accountGroupId, null)

        def incomeExpenseType = [
            (IncomeExpenseTypeConstant.EXPENSE): "支出",
            (IncomeExpenseTypeConstant.INCOME): "收入"
        ]

        def paymentType = [
            (PaymentTypeConstant.CASH): "现金",
            (PaymentTypeConstant.CREDIT_CARD): "信用卡"
        ]

        sendDataResponse([
            accountGroupInfo: accountGroupInfo,
            bankCards: bankCards,
            detailTypes: detailTypes,
            incomeExpenseType: incomeExpenseType,
            paymentType: paymentType,
            recentSearchListMaxCount: recentSearchListMaxCount,
            baiduMap: [
                androidKey: baiduMapAndroidKey,
            ],
            amap: [
                androidKey: amapAndroidKey,
                webApiKey: amapWebApiKey,
            ],
        ])
    }
}